package com.leyou.controller;

import com.leyou.pojo.SpecGroup;
import com.leyou.pojo.SpecParam;
import com.leyou.service.SpecificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/spec")
public class SpecificationController {

    @Autowired
    private SpecificationService specificationService;

    @GetMapping("/groups/{cid}")
    public ResponseEntity<List<SpecGroup>> querySpecGroupByCid(@PathVariable("cid") Long cid) {
        return ResponseEntity.ok(specificationService.querySpecGroupByCid(cid));
    }

    @GetMapping("/params")
    public ResponseEntity<List<SpecParam>> querySpecParamList(
            @RequestParam(value = "gid", required = false) Long gid,
            @RequestParam(value = "cid", required = false) Long cid,
            @RequestParam(value = "searching", required = false) Boolean searching,
            @RequestParam(value = "generic", required = false) Boolean generic) {
        return ResponseEntity.ok(specificationService.querySpecParamList(gid, cid, searching, generic));
    }

    /**
     * 新增规格组
     *
     * @param group
     * @return
     */
    @PostMapping("/group")
    public ResponseEntity<Void> addSpecGroup(SpecGroup group) {
        specificationService.addSpecGroup(group);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    /**
     * 修改规格组
     *
     * @param group
     * @return
     */
    @PutMapping("/group")
    public ResponseEntity<Void> updSpecGroup(SpecGroup group) {
        specificationService.updSpecGroup(group);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    /**
     * 删除规格组
     *
     * @param id
     * @return
     */
    @DeleteMapping("/group/{id}")
    public ResponseEntity<Void> delSpecGroup(@PathVariable("id") Long id) {
        specificationService.delSpecGroup(id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 新增规格参数
     *
     * @param param
     * @return
     */
    @PostMapping("/param")
    public ResponseEntity<Void> addSpecParam(SpecParam param) {
        specificationService.addSpecParam(param);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    /**
     * 修改规格参数
     *
     * @param param
     * @return
     */
    @PutMapping("/param")
    public ResponseEntity<Void> updSpecParam(SpecParam param) {
        specificationService.updSpecParam(param);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    /**
     * 删除规格参数
     *
     * @param id
     * @return
     */
    @DeleteMapping("/param/{id}")
    public ResponseEntity<Void> delSpecParam(@PathVariable("id") Long id) {
        specificationService.delSpecParam(id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

}
